<?
 class Journal
 {

    var $db;
	var $tbl_journal;
	var $tbl_comments;

    function Journal($db, $tbl_journal, $tbl_comments)
    {
        $this->db = $db;
	    $this->tbl_journal = $tbl_journal;
	    $this->tbl_comments = $tbl_comments;
	}

//----------------mototype

    function SelectMototype()
    {
       $query="SELECT * FROM $this->tbl_mototype ORDER BY id";
       $arr = $this->db->ArrFromQuery($query);
       return $arr;
    }

	function SelectOneMototype($id)
    {
       $query="SELECT * FROM $this->tbl_mototype WHERE id='".$id."'";
       $arr=$this->db->ArrFromQuery($query);
       return $arr;
    }
//----------------Journal

	function insertJournal($post, $files='')
	{
		$exept_ar = array("jrn_act", "jrnId");
		$post['name'] = htmlspecialchars($post['name']);
		$q = $this->db->MakeQuery($post,$exept_ar);
		$date = time();
		$query = "INSERT INTO $this->tbl_journal SET ".$q.", date=$date, updated=$date";
		$id = $this->db->RunQueryGetId($query);
		return $id;
	}

	function selectJournalByUser($userId, $limit="")
    {
	   if ($limit!="")
	      $limit = " LIMIT ".$limit;
       $query="SELECT * FROM $this->tbl_journal WHERE user_id=$userId ORDER BY date DESC".$limit;
	   $arr=$this->db->ArrFromQuery($query);
       return $arr;
    }

	function selectAllJournals($limit="")
    {
	   if ($limit != "")
	   {
	      $limit = " LIMIT ".$limit;
       }
	   $query = "SELECT * FROM $this->tbl_journal ORDER BY updated DESC".$limit;
	   $arr = $this->db->ArrFromQuery($query);
       return $arr;
    }

	function selectUserByJournal($jrnId)
    {
       $query="SELECT user_id FROM $this->tbl_journal WHERE id=$jrnId";
	   $arr=$this->db->ArrFromQuery($query);
       return $arr;
    }

	function selectJournals($limit="")
    {
	   if ($limit!="")
	   {
	      $limit = " LIMIT ".$limit;
       }
	   $query="SELECT * FROM $this->tbl_journal ORDER BY updated DESC".$limit;
	   $arr=$this->db->ArrFromQuery($query);
       return $arr;
    }

    function selectOneJournal($id)
    {
       $query="SELECT * FROM $this->tbl_journal WHERE id='".$id."'";
       $arr=$this->db->ArrFromQuery($query);
       return $arr;
    }

	function deleteJournal($jrnId)
    {
		$this->deleteCommentsByJournal($jrnId);
       $query = "DELETE FROM $this->tbl_journal WHERE id='$jrnId'";
	   $this->db->RunQuery($query);
    }

    function updateJournal($post)
    {
		$exept_ar = array("jrn_act", "jrnId");
		$post['name'] = htmlspecialchars($post['name']);
		$date = time();
		$q = $this->db->MakeQuery($post,$exept_ar);
       $query = "UPDATE $this->tbl_journal SET $q, updated=$date WHERE id='".$post['jrnId']."'";
       $this->db->RunQuery($query);
    }

	function journalView($jrnId)
	{
		$query = "UPDATE $this->tbl_journal SET view=view+1 WHERE id=$jrnId";
		$this->db->RunQuery($query);
	}
//----------------comments

	function insertComment($post)
	{
		$exept_ar = array("jrn_act");
		$post['text'] = htmlspecialchars($post['text']);
		$q = $this->db->MakeQuery($post,$exept_ar);
		$date = time();
		$query = "INSERT INTO $this->tbl_comments SET ".$q.", date=$date";
		$id = $this->db->RunQueryGetId($query);
		return $id;
	}

	function selectComments($jrnId)
    {
       $query = "SELECT * FROM $this->tbl_comments WHERE jrn_id='$jrnId' ORDER BY date DESC";
       $ar = $this->db->ArrFromQuery($query);
       return $ar;
    }

	function deleteCommentsByJournal($jrnId)
	{
       $query = "DELETE FROM $this->tbl_comments WHERE jrn_id='$jrnId'";
       $ar = $this->db->RunQuery($query);
	}
 }
?>
